/**
 * Class: ProjectionModel
 * Keeps track of:
 * - resolution
 * - xy center
 *
 * Inherits:
 *
 * NOTE: Coordinate origin is left-top corner.
 */
var ProjectionModel = SMap.Class(Model, {
	// private
	updateCenter: function() {
		var ll = this.map.getCenterLonLat(), b = this.map.getBoundsLonLat(), res = this.resolution;
		this.center = new XY((ll.lon - b.left) / res, (b.top - ll.lat) / res);
	},
	// public
	resolution: 1, // ll per xy
	/**
	 * Constructor: init
	 * 
	 * Class constructor.
	 * 
	 * Parameters: 
	 * map - <MapModel>
	 */
	init: function(map) {
		Model.prototype.init.apply(this, arguments);
		
		this.map = map;
		
		map.listeners.add("move", this.updateCenter, this);
		
		this.updateCenter();
	},
	setResolution: function(res) {
		this.resolution = res;
		this.updateCenter();
	},
	/**
	 * Method: setCenterXY
	 * Sets center coordinates in xy units
	 * 
	 * Parameters:
	 * 	 ll - <XY>
	 */
	setCenterXY: function(xy) {
		if (!this.center.equal(xy)) {
			var res = this.resolution, b = this.map.getBoundsLonLat();
			this.map.setCenterLonLat(new LonLat(b.left + (xy.x * res), b.top - xy.y * res));
		}
	},
	/**
	 * Method: getCenterXY
	 * 
	 * Returns:
	 *   <XY> - Current center coordinates in xy units
	 */
	getCenterXY: function() {
		return this.center.clone();
	},
	getBoundsXY: function() {
		var b = this.map.getBoundsLonLat(), res = this.resolution;
		return new Bounds(0, (b.top - b.bottom) / res, (b.right - b.left) / res, 0);
	},
	toString: function() {
		return format("[ProjectionModel: center - {center}]", this);
	}
});
